function nv = normvecs(v)
% normvecs  - normalize vector lengths to 1
%
% FORMAT:       nv = normvecs(v)
%
% Input fields:
%
%       v           Vx3 vectors
%
% Output fields:
%
%       nv          normalized vectors

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 1 || ...
   ~isa(v, 'double') || ...
    ndims(v) > 2 || ...
    size(v, 2) ~= 3
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing argument.' ...
    );
end

% computation
nv = v ./ repmat(sqrt(sum(v .* v, 2)), [1, 3]);

% and error removal
nvn = find(any(isinf(nv) | isnan(nv), 2));
if ~isempty(nvn)
    nv(nvn, :) = repmat([0, 0, 1], [numel(nvn), 1]);
end
